package com.sara.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.sara.dto.ResponseBean;
import com.sara.entity.*;
import com.sara.exception.ServiceException;
import com.sara.service.OrderService;
import com.sara.service.PartsService;
import com.sara.service.TypeService;
import com.sara.vo.OrderInfoVo;
import com.sara.vo.OrderVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Wayne
 * @date 2019/1/11
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private PartsService partsService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/add")
    public String addOrderPage(){
        return "order/add";
    }


    @GetMapping("/service/types")//狗子add页面的选择服务项目选择框中的值
    @ResponseBody
    public ResponseBean serviceTpePage(){
        List<ServiceType> serviceTypeList = orderService.findAllServiceType();
        return  ResponseBean.success(serviceTypeList);
    }

    @GetMapping("/parts/types")//狗子add页面的选择配件选择框中的服务类型
    @ResponseBody
    public ResponseBean partstypePage(){
        List<Type> typeList = typeService.findAll();
        return ResponseBean.success(typeList);
    }

    @PostMapping("/type")
    @ResponseBody
    public ResponseBean partsByType(Integer typeId){
        List<Parts> partsList = partsService.findpartsByTypeId(typeId);
        if (partsList == null || partsList.size() == 0) {
            return ResponseBean.error("暂无符合该类型的配件!");
        }
        return ResponseBean.success(partsList);
    }


    @PostMapping("/add")
    @ResponseBody
    public ResponseBean addOrder(String json){
        Gson gson = new Gson();
        OrderVo orderVo = gson.fromJson(json, OrderVo.class);

        System.out.println("orderVo" + orderVo);

        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();

        orderService.saveOrder(orderVo, employee.getId());

        return ResponseBean.success();
    }


    @GetMapping("/undone/list")
    public String unListPage(@RequestParam(name = "p",defaultValue = "1",required = false) Integer pageNo,
                    @RequestParam(required = false) String licenceNo,
                    @RequestParam(required = false) String tel,
                    @RequestParam(required = false) String startTime,
                    @RequestParam(required = false) String endTime,
                    Model model ){
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("pageNo", pageNo);
        queryMap.put("licenceNo", licenceNo);
        queryMap.put("tel", tel);
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("exState", Order.ORDER_STATE_DONE);
        PageInfo<Order> page = orderService.findPageByParam(queryMap);
        model.addAttribute("type","");
        model.addAttribute("page", page);


        return "order/unList";
    }


    @GetMapping("/done/list")
    public String doneListPage(
            @RequestParam(name = "p",defaultValue = "1",required = false) Integer pageNo,
            @RequestParam(required = false) String licenceNo,
            @RequestParam(required = false) String tel,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            Model model){

        // 封装筛选的queryMap集合
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("pageNo", pageNo);
        queryMap.put("licenceNo", licenceNo);
        queryMap.put("tel", tel);
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("state", Order.ORDER_STATE_DONE);

        PageInfo<Order> page = orderService.findPageByParam(queryMap);

        model.addAttribute("type","done");
        model.addAttribute("page", page);

        return "order/doneList";
    }

    @GetMapping("/detail/{id:\\d+}")
    public String detailPage(@PathVariable Integer id, Model model){
        //获取订单信息
        Order order = orderService.findOrderById(id);

        //获取订单服务类型信息

        ServiceType serviceType = orderService.findServiceTypeById(order.getServiceTypeId());


        //获取配件订单列表

        List<Parts> partsList = partsService.findPartsByOrderId(order.getId());

        model.addAttribute("order", order);
        model.addAttribute("serviceType", serviceType);
        model.addAttribute("partsList", partsList);
        return "order/detail";

    }


    @GetMapping("/edit/{id:\\d+}")
    public String editPage(@PathVariable Integer id, Model model){
        Map<String, Object> editMap = orderService.findEditByOrderId(id);


       /* System.out.println("editMap" + editMap);
        model.addAttribute("editMap",editMap);*/
        model.addAttribute("orderId", id);
        return "order/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public ResponseBean editPage(String json){
        Gson gson = new Gson();
        OrderVo orderVo = gson.fromJson(json, OrderVo.class);
        orderService.editOrder(orderVo);
        return ResponseBean.success();
    }


    @ResponseBody
    @GetMapping("/info/{id:\\d+}")
    public ResponseBean info(@PathVariable Integer id){
        //获取订单

        //获得服务类型
        //获得零件
        Order order = orderService.findOrderById(id);
        ServiceType serviceType = orderService.findServiceTypeById(order.getServiceTypeId());

        List<Parts> partsList = partsService.findPartsByOrderId(order.getId());

        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setOrder(order);
        orderInfoVo.setPartsList(partsList);
        orderInfoVo.setServiceType(serviceType);
        return ResponseBean.success(orderInfoVo);
    }


    @GetMapping("/del/{id:\\d+}")
    public String  deleteOrder(@PathVariable Integer id , RedirectAttributes attributes){
        orderService.deleteOrder(id);
        return "redirect:/order/undone/list";
    }


    @ResponseBody
    @GetMapping("/trance/{id:\\d+}")
    public ResponseBean trance(@PathVariable Integer id){
        try {
            orderService.transOrder(id, Order.ORDER_STATE_TRANS);

        } catch (ServiceException e) {
            return ResponseBean.error(e.getMessage());
        }
        return ResponseBean.success();
    }




}
